package com.dhcc.DRGsHosp.application.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Named;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.transaction.annotation.Transactional;

import com.dhcc.DRGsHosp.application.ReDisGroupApplication;

@Named
@Transactional
public class ReDisGroupApplicationImpl implements ReDisGroupApplication {
	@Autowired
	private NamedParameterJdbcTemplate jdbcTempSeverice;
	/**
	 *  执行查询SQL
	 *  executeQueryForList.  
	 *  @Description：
	 *  @Author ChenHanlin
	 *  @param sql
	 *  @param obj
	 *  @return 
	 *  @Date:2017年10月12日下午2:40:26
	 */
	@Override
	public List<Map<String, Object>> queryForList(String sql,Object obj) {
		SqlParameterSource paramSource = new BeanPropertySqlParameterSource(obj);
		List<Map<String, Object>> mapLsit = jdbcTempSeverice.queryForList(sql, paramSource)  ;
		return mapLsit;
	}
	
	/**
	 *  执行更新SQL
	 *  executeUpdate.  
	 *  @Description：
	 *  @Author ChenHanlin
	 *  @param sql
	 *  @param condition
	 *  @return 
	 *  @Date:2017年10月12日下午2:40:45
	 */
	@Override
	public int executeUpdate(String sql, Map<String, Object> condition) {
		return jdbcTempSeverice.update(sql, condition) ;
	}
	@Override
	public int addReDisGroup(Map<String, Object> condition) {
		StringBuffer sql = new StringBuffer(); 
		condition.put("log", condition.get("log") + "") ;
		sql.append(" insert into drgs.mr_redisgroup rg (rg.redis_id,rg.pmainindex,rg.diasis_code, \n ") ;
		sql.append(" rg.drg_code,rg.drg_weight,rg.ver_code,rg.is_check,rg.log,rg.diasis_order)  \n") ;
		sql.append(" values (:redisId,:pmainindex,:diasisCode,:drgCode,:weight,:verCode,:isCheck,:log,:diasisOrder) ") ;
		try {
			return jdbcTempSeverice.update(sql.toString() , condition) ;
		} catch (Exception e) {
			System.out.println("保存主键为" + condition.get("redisId")+"的时候报错了");
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public int updateReDisGroup(Map<String, Object> condition) {
		StringBuffer sql = new StringBuffer(); 
		condition.put("log", condition.get("log") + "") ;
		sql.append(" update drgs.MR_REDISGROUP t set t.pmainindex = :pmainindex, t.diasis_code = :diasisCode ,t.drg_code = :drgCode, \n ") ;
		sql.append(" t.drg_weight = :weight,t.ver_code = :verCode,t.is_check = :isCheck,t.log = :log,t.diasis_order = :diasisOrder \n ") ;
		sql.append(" where t.redis_id = :redisId ") ;
		return jdbcTempSeverice.update(sql.toString() , condition) ;
	}

	@Override
	public List<Map<String, Object>> findReDisGroupByCaseId(String pmainindex) {
		StringBuffer sql = new StringBuffer(); 
		sql.append(" select t.redis_id,t.diasis_code,t.drg_code,t.drg_weight, \n ") ;
		sql.append(" t.ver_code,t.is_check,t.log,t.diasis_order \n ") ;
		sql.append(" from drgs.MR_REDISGROUP t \n ") ;
		sql.append(" where t.pmainindex = :pmainindex ") ;
		Map<String,Object> condition  = new HashMap<>();
		condition.put("pmainindex", pmainindex) ;
		return jdbcTempSeverice.queryForList(sql.toString(), condition) ;
	}

	@Override
	public Double findWeightByDrgCodeAndGroupVer(String drgCode, String groupVer,String benchType) {
		StringBuffer sql = new StringBuffer(); 
		Map<String,Object> condition  = new HashMap<>();
		condition.put("drgCode", drgCode);
		condition.put("groupVer", groupVer);
		String conditionSql="";
		if(benchType!=null){
			conditionSql="and as_std_type=:benchType";
			condition.put("benchType", benchType);
		}
		sql.append(" select as_weight from drgs.t_annualstd    \n ") ;
		sql.append(" where ver_code = :groupVer "+conditionSql+" and as_drg_code =:drgCode and as_year=( \n ") ;
		sql.append(" SELECT max(as_year) from drgs.t_annualstd where ver_code = :groupVer  "+conditionSql+" ) \n ") ;
		List<Map<String,Object>> list =  jdbcTempSeverice.queryForList(sql.toString(), condition) ;
		Double weight = null ;
		if(list != null && list.size() > 0) {
			weight = list.get(0).get("AS_WEIGHT")==null?null:new Double(list.get(0).get("AS_WEIGHT").toString()) ;
		}
		return weight ;
	}

	@Override
	public void saveReDisGroupList(List<Map<String, Object>> allReGroupInfoList) {
		String pmainindex = allReGroupInfoList.get(0).get("pmainindex").toString();
		// 查询病例再分组结果
		List<Map<String,Object>> reDisGroupList = findReDisGroupByCaseId(pmainindex) ;
		List<String> redisIdList = new ArrayList<>() ;
		for (Map<String,Object> map : reDisGroupList) {
			redisIdList.add(map.get("REDIS_ID").toString());
		}
		// 记录再分组后入组结果
		for(Map<String,Object> map : allReGroupInfoList) {
			if (redisIdList.contains(map.get("redisId").toString()) ) {
				updateReDisGroup(map) ;
			} else {
				addReDisGroup(map) ;
			}
		}
	}
}
